<?php

namespace App\Library\Logic;

use App\Models\OpenIdModel;
use Illuminate\Http\JsonResponse;

class OpenIdLogic extends OpenIdModel
{
    public static function bindOpenId(int $userId): void
    {
        $all = request()->all();
        if (!isset($all['phoneCode']) || !isset($all['code'])) return;
        else if (OpenIdLogic::existsByWhere([
            'userId' => $userId,
            'plateform' => $all['plateform'] ?? 2,
            'type' => $all['type'] ?? 'mini',
        ])) return;
        $data = loginByCode($all['code'], $all['type'] ?? 'mini');
        if ($data instanceof JsonResponse) return;
        OpenIdLogic::insertForModel([
            'userId' => $userId,
            'plateform' => $all['plateform'] ?? 2,
            'type' => $all['type'] ?? 'mini',
            'openid' => $data['openid'],
            'unionid' => $data['unionid'],
        ]);
    }

    /**
     * isFollow
     * @return bool
     */
    public static function isFollow(): bool
    {
        return self::existsByWhere(['userId' => USERID, 'plateform' => 2, 'type' => 'wx', 'isFollow' => 1]);
    }
}
